package com.guanglan.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateTimeUtil{
	/**
	 * 得到当前日期
	 * @return 返回当前日期Date
	 */
	public static Date getCurrentDate() {
		return new Date(System.currentTimeMillis());
	}
	
	/**
	 * 根据pattern转换日期对象为日期字符串
	 * 
	 * @param date
	 *            日期
	 * @param pattern
	 *            日期格式(如"yyyy-MM-dd hh:mm:ss")
	 * @return 日期字串
	 */
	public static String formatDateTimeToString(Date date, String pattern) {

		String returnString = null;// 默认返回值
		if ((date == null) || (pattern == null) || pattern.trim().equalsIgnoreCase("")) {
			returnString = "";
		} else {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
			simpleDateFormat.setLenient(false);
			returnString = simpleDateFormat.format(date);
		}

		return returnString;
	}
	
	/**
	 * 根据pattern转换日期字符串为日期对象,
	 * 
	 * @param date
	 *            日期
	 * @param pattern
	 *            日期格式(如"yyyy-MM-dd hh:mm:ss")
	 * @return 日期字串
	 */
	public static Date formatStringToDate(String dateString, String pattern) throws ParseException {

		Date returnDate = null;// 默认返回值

		if ((dateString == null) || (pattern == null) || dateString.trim().equalsIgnoreCase("")
				|| pattern.trim().equalsIgnoreCase("") || (dateString.trim().length() != pattern.trim().length())) {
			returnDate = null;
		} else {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
			simpleDateFormat.setLenient(false);
			returnDate = simpleDateFormat.parse(dateString);
		}
		return returnDate;

	}
	
	/**  
     * 计算两个日期之间相差的天数  
     * @param smdate 较小的时间 
     * @param bdate  较大的时间 
     * @return 相差天数 
     * @throws ParseException  
     */    
    public static int daysBetween(Date smdate,Date bdate) throws ParseException    
    {    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        smdate=sdf.parse(sdf.format(smdate));  
        bdate=sdf.parse(sdf.format(bdate));  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(smdate);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(bdate);    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));           
    }  
    
    /**
     * 获取当前月有多少天
     * @return
     */
    public static int getDayOfMonth(){
    	 Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
    	 int day=aCalendar.getActualMaximum(Calendar.DATE);
    	 return day;
    }
    
    /**
     * @author wangzhiyong
     * @param 判断今天周几
     * @return
     */
    public static int getWeekOfDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        
        if(0==w)w=7;
        
        return w;
    }
    
    //获取前后日期 i为正数 向后推迟i天，负数时向前提前i天
  	public static String getDate(int i){
  		Date dat = null;
  		Calendar cd = Calendar.getInstance();
  		cd.add(Calendar.DATE, i);
  		dat = cd.getTime();
  		SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  		return dformat.format(dat);
  	}
  	
  	
	public static void main(String[] args) throws ParseException {
		//int a = daysBetween(formatStringToDate("2017-02-01 17:52:10","yyyy-MM-dd HH:mm:ss"),new Date());
	}
}
